package com.example.controller;

import com.example.dao.DiscussDao;
import com.example.dao.IcommentDao;
import com.example.model.pojo.Comment;
import com.example.model.pojo.Discussion;
import com.example.model.pojo.Result;
import com.example.model.pojo.UserInfo;
import com.example.websocket.WebSocketProcess;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("webs")
public class WebSocketController {
    @Autowired
    private WebSocketProcess webSocketProcess;
    @Autowired
    private IcommentDao icommentDao;
    @Autowired
    private DiscussDao discussionDao;

    @Autowired
    private RedisTemplate redisTemplate;
    @GetMapping("reply")
    public Result hello(Integer id, String msg, HttpServletRequest request)  {
        Discussion discussion=discussionDao.getById(id);
        Integer touid=discussion.getUid();
        UserInfo userInfo= (UserInfo) request.getAttribute("userInfo");
        redisTemplate.opsForValue().set(String.valueOf(id),msg);
        redisTemplate.convertAndSend("chart",String.valueOf(touid));
        Comment comment=new Comment();
        comment.setContent(msg);
        comment.setUuId(userInfo.getUuid());
        comment.setDiscussId(id);
        comment.setCStatus(1);
        comment.setStatus(1);
        icommentDao.save(comment);
       return new Result("200","success");
    }

}
